/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.phone.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.phone.dao.SalesRecordsDao;
import com.jeesite.modules.phone.entity.PhoneCommodity;
import com.jeesite.modules.phone.entity.Staff;
import com.jeesite.modules.phone.service.PhoneCommodityService;
import com.jeesite.modules.phone.service.StaffService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.phone.entity.SalesRecords;
import com.jeesite.modules.phone.service.SalesRecordsService;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 销售记录Controller
 * @author lzw
 * @version 2019-04-03
 */
@Controller
@RequestMapping(value = "${adminPath}/phone/salesRecords")
public class SalesRecordsController extends BaseController {

	@Autowired
	private SalesRecordsService salesRecordsService;
	@Autowired
	private PhoneCommodityService phoneCommodityService;
	@Autowired
	private StaffService staffService;
	@Autowired
	private SalesRecordsDao salesRecordsDao;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public SalesRecords get(Long id, boolean isNewRecord) {
		return salesRecordsService.get(id+"", isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = {"list", ""})
	public String list(SalesRecords salesRecords, Model model) {
		model.addAttribute("salesRecords", salesRecords);
		return "modules/phone/salesRecordsList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<SalesRecords> listData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
		Page<SalesRecords> page = salesRecordsService.findPage(new Page<SalesRecords>(request, response), salesRecords); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = "form")
	public String form(SalesRecords salesRecords, Model model) {
		model.addAttribute("salesRecords", salesRecords);
		model.addAttribute("phoneCommodityList",phoneCommodityService.findList(new PhoneCommodity()));
		model.addAttribute("staffList",staffService.findList(new Staff()));
		return "modules/phone/salesRecordsForm";
	}

	/**
	 * 保存销售记录
	 */
	@RequiresPermissions("phone:salesRecords:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated SalesRecords salesRecords) {
		salesRecordsService.save(salesRecords);
		return renderResult(Global.TRUE, "保存销售记录成功！");
	}
	
	/**
	 * 删除销售记录
	 */
	@RequiresPermissions("phone:salesRecords:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(SalesRecords salesRecords) {
		salesRecordsService.delete(salesRecords);
		return renderResult(Global.TRUE, "删除销售记录成功！");
	}

	/**
	 * 按月查询销售列表
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = {"monthlist", ""})
	public String monthlist(SalesRecords salesRecords, Model model) {
		model.addAttribute("salesRecords", salesRecords);
		return "modules/phone/monthSalesList";
	}

	/**
	 * 按月查询列表数据
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = "monthlistData")
	@ResponseBody
	public Page<SalesRecords> monthlistData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
		Page<SalesRecords> page = new Page<SalesRecords>();
		if(salesRecords!=null&&salesRecords.getYrars()!=""&&salesRecords.getYrars()!=null){
			page.setList(salesRecordsDao.obtainMonthInfo(salesRecords.getYrars()));
		}else{
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			page.setList(salesRecordsDao.obtainMonthInfo(sdf.format(date)));
		}
		page.setPageSize(0);
		page.setPageNo(0);
		return page;
	}
    /**
     * 查询厂商月记录列表
     */
    @RequiresPermissions("phone:salesRecords:view")
    @RequestMapping(value = {"manufacturerMonthlist", ""})
    public String manufacturerMonthlist(SalesRecords salesRecords, Model model) {
        salesRecords.setMonths(salesRecords.getYrars().substring(5,7));
        salesRecords.setYrars(salesRecords.getYrars().substring(0,4));
        model.addAttribute("salesRecords", salesRecords);
        return "modules/phone/salesManufacturermonthRecordList";
    }

    /**
     * 查询厂商月记录列表数据
     */
    @RequiresPermissions("phone:salesRecords:view")
    @RequestMapping(value = "manufacturerMonthlistData")
    @ResponseBody
    public Page<SalesRecords> manufacturerMonthlistData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
        Page<SalesRecords> page = new Page<SalesRecords>();
        page.setList(salesRecordsDao.obtainManufacturerMonthInfo(salesRecords.getYrars(),salesRecords.getMonths()));
        page.setPageSize(30);
        return page;
    }

    /**
     * 查询商品月记录列表
     */
    @RequiresPermissions("phone:salesRecords:view")
    @RequestMapping(value = {"commodityMonthlist", ""})
    public String commodityMonthlist(SalesRecords salesRecords, Model model) {
        salesRecords.setMonths(salesRecords.getYrars().substring(5,7));
        salesRecords.setYrars(salesRecords.getYrars().substring(0,4));
        model.addAttribute("salesRecords", salesRecords);
        return "modules/phone/salesCommodityMonthList";
    }

    /**
     * 查询商品月记录列表数据
     */
    @RequiresPermissions("phone:salesRecords:view")
    @RequestMapping(value = "commodityMonthlistData")
    @ResponseBody
    public Page<SalesRecords> commodityMonthlistData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
        Page<SalesRecords> page = new Page<SalesRecords>();
        page.setList(salesRecordsDao.obtainCommodityMonthInfo(salesRecords.getYrars(),salesRecords.getMonths(),salesRecords.getProductManufacturerId()));
        page.setPageSize(30);
        return page;
    }
	/**
	 * 查询当日销售列表
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = {"daylist", ""})
	public String daylist(SalesRecords salesRecords, Model model) {
		model.addAttribute("salesRecords", salesRecords);
		return "modules/phone/todaySalesList";
	}

	/**
	 * 查询当日列表数据
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = "daylistData")
	@ResponseBody
	public Page<SalesRecords> daylistData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
		Page<SalesRecords> page = new Page<SalesRecords>();
		page.setList(salesRecordsDao.obtainDayInfo());
		page.setPageSize(0);
		page.setPageNo(0);
		return page;
	}
    /**
     * 查询厂商日记录列表
     */
    @RequiresPermissions("phone:salesRecords:view")
    @RequestMapping(value = {"manufacturerDaylist", ""})
    public String manufacturerDaylist(SalesRecords salesRecords, Model model) {
        model.addAttribute("salesRecords", salesRecords);
        return "modules/phone/salesManufacturerDayList";
    }

    /**
     * 查询厂商日记录列表数据
     */
    @RequiresPermissions("phone:salesRecords:view")
    @RequestMapping(value = "manufacturerDaylistData")
    @ResponseBody
    public Page<SalesRecords> manufacturerDaylistData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
        Page<SalesRecords> page = new Page<SalesRecords>();
        page.setList(salesRecordsDao.obtainManufacturerDayInfo());
        page.setPageSize(0);
        page.setPageNo(0);
        return page;
    }

    /**
     * 查询商品日记录列表
     */
    @RequiresPermissions("phone:salesRecords:view")
    @RequestMapping(value = {"commodityDaylist", ""})
    public String commodityDaylist(SalesRecords salesRecords, Model model) {
        model.addAttribute("salesRecords", salesRecords);
        return "modules/phone/salesCommodityDayList";
    }

    /**
     * 查询商品日记录列表数据
     */
    @RequiresPermissions("phone:salesRecords:view")
    @RequestMapping(value = "commodityDaylistData")
    @ResponseBody
    public Page<SalesRecords> commodityDaylistData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
        Page<SalesRecords> page = new Page<SalesRecords>();
        page.setList(salesRecordsDao.obtainCommodityDayInfo());
        page.setPageSize(30);
        return page;
    }
	/**
	 * 按季度查询销售列表
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = {"quarterlist", ""})
	public String quarterlist(SalesRecords salesRecords, Model model) {
		model.addAttribute("salesRecords", salesRecords);
		return "modules/phone/quarterSalesList";
	}

	/**
	 * 按季度查询列表数据
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = "quarterlistData")
	@ResponseBody
	public Page<SalesRecords> quarterlistData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
		Page<SalesRecords> page = new Page<SalesRecords>();
		if(salesRecords!=null&&salesRecords.getYrars()!=""&&salesRecords.getYrars()!=null){
			page.setList(salesRecordsDao.obtainQuarterInfo(salesRecords.getYrars()));
		}else{
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			page.setList(salesRecordsDao.obtainQuarterInfo(sdf.format(date)));
		}
		page.setPageSize(0);
		page.setPageNo(0);
		return page;
	}

	/**
	 * 查询厂家季度记录列表
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = {"manufacturerQuarterlist", ""})
	public String manufacturerQuarterlist(SalesRecords salesRecords, Model model) {
		model.addAttribute("salesRecords", salesRecords);
		return "modules/phone/salesManufactureQuarterRecordList";
	}

	/**
	 * 查询厂家季度记录列表数据
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = "manufacturerQuarterlistData")
	@ResponseBody
	public Page<SalesRecords> manufacturerQuarterlistData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
		Page<SalesRecords> page = new Page<SalesRecords>();
		page.setList(salesRecordsDao.obtainManufacturerQuarterInfo(salesRecords.getYrars(),salesRecords.getQuarters()));
		page.setPageSize(30);
		return page;
	}

	/**
	 * 查询商品季度记录列表
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = {"commodityQuarterlist", ""})
	public String commodityQuarterlist(SalesRecords salesRecords, Model model) {
		model.addAttribute("salesRecords", salesRecords);
		return "modules/phone/salesCommodityQuarterRecordList";
	}

	/**
	 * 查询商品季度记录列表数据
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = "commodityQuarterlistData")
	@ResponseBody
	public Page<SalesRecords> commodityQuarterlistData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
		Page<SalesRecords> page = new Page<SalesRecords>();
		page.setList(salesRecordsDao.obtainCommodityQuarterInfo(salesRecords.getYrars(),salesRecords.getQuarters(),salesRecords.getProductManufacturerId()));
		page.setPageSize(30);
		return page;
	}
	/**
	 * 按年度查询销售列表
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = {"yearlist", ""})
	public String yearlist(SalesRecords salesRecords, Model model) {
		model.addAttribute("salesRecords", salesRecords);
		return "modules/phone/yearSalesList";
	}

	/**
	 * 按年度查询列表数据
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = "yearlistData")
	@ResponseBody
	public Page<SalesRecords> yearlistData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
		Page<SalesRecords> page = new Page<SalesRecords>();
		if(salesRecords!=null&&salesRecords.getYrars()!=""&&salesRecords.getYrars()!=null){
			page.setList(salesRecordsDao.obtainYearInfo(salesRecords.getYrars()));
		}else{
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			page.setList(salesRecordsDao.obtainYearInfo(sdf.format(date)));
		}
		page.setPageSize(0);
		page.setPageNo(0);
		return page;
	}

	/**
	 * 查询厂商年记录列表
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = {"manufacturerYearlist", ""})
	public String manufacturerYearlist(SalesRecords salesRecords, Model model) {
		model.addAttribute("salesRecords", salesRecords);
		return "modules/phone/salesManufacturerYearRecordList";
	}

	/**
	 * 查询厂商年记录列表数据
	 */
	@RequiresPermissions("phone:phoneCommodity:view")
	@RequestMapping(value = "manufacturerYearlistData")
	@ResponseBody
	public Page<SalesRecords> manufacturerYearlistData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
		Page<SalesRecords> page = new Page<SalesRecords>();
		page.setList(salesRecordsDao.obtainManufacturerYearInfo(salesRecords.getYrars()));
		page.setPageSize(30);
		return page;
	}


	/**
	 * 查询商品年记录列表
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = {"commodityYearlist", ""})
	public String commodityYearlist(SalesRecords salesRecords, Model model) {
		model.addAttribute("salesRecords", salesRecords);
		return "modules/phone/salesCommodityYearRecordList";
	}

	/**
	 * 查询商品年记录列表数据
	 */
	@RequiresPermissions("phone:salesRecords:view")
	@RequestMapping(value = "commodityYearlistData")
	@ResponseBody
	public Page<SalesRecords> commodityYearlistData(SalesRecords salesRecords, HttpServletRequest request, HttpServletResponse response) {
		Page<SalesRecords> page = new Page<SalesRecords>();
		page.setList(salesRecordsDao.obtainCommodityYearInfo(salesRecords.getYrars(),salesRecords.getProductManufacturerId()));
		page.setPageSize(30);
		return page;
	}
}